<?php
require_once 'connect.php';
$res_web = get_url('https://esi.evepc.163.com/latest/incursions/?datasource=serenity');
if (!$res_web) {
    echo '网络错误';
    mysqli_close($sqlconnect);
    exit;
}
$json = json_decode($res_web);
$json_err = json_last_error();
if ($json_err != JSON_ERROR_NONE) {
    echo 'json解析错误';
    mysqli_close($sqlconnect);
    exit;
}
echo "<h1>EVE online萨沙入侵</h1><hr/>";
for ($i = 1; $i <= count($json); $i++) {
    $constellation_id = $json[$i - 1]->constellation_id;
    $state = $json[$i - 1]->state;
    $sql = "SELECT * FROM 星系列表 WHERE 星座ID='" . $constellation_id . "'";
    $res = mysqli_query($sqlconnect, $sql);
    $res2 = $res->fetch_all();
    switch ($state) {
        case 'established':
            $state = '确立';
            break;
        case 'mobilizing':
            $state = '动员';
            break;
        case 'withdrawing':
            $state = '撤退';
    }
    if ($json[$i - 1]->has_boss) {
        $has_boss = "有BOSS";
    } else {
        $has_boss = "无BOSS";
    }
    $text = "<table border='2'><thead><tr><th>" . $res2[0][5] . "</th>
        <th>" . $state . "</th>
        <th>" . $has_boss . "</th>
        </tr></thead><tbody><tr>";

    $num_1 = count($json[$i - 1]->infested_solar_systems);
    for ($i2 = 1; $i2 <= $num_1; $i2++) {
        $sql = "SELECT * FROM 星系列表 WHERE 星系ID='" . $json[$i - 1]->infested_solar_systems[$i2 - 1] . "'";
        $res = mysqli_query($sqlconnect, $sql);
        $res2 = $res->fetch_all();
        $security_level = $res2[0][6];
        $security_level = number_format($security_level, 1);
        $text = $text . "<td>" . $res2[0][1] . "(" . $security_level . ")</td>";


    }
    $text = $text . "</tr></tbody><br/>";
    echo $text;
}
mysqli_close($sqlconnect);
function get_url($url): string
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}